package com.yxd.bdmapdemo.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;


/**
 * Created by Administrator on 2017/5/12.
 */

public class UserManager {

    private UserManager() {
    }

    private UserInfo user;

    private static UserManager instance;

    public static UserManager getInstance() {
        if (instance == null) {
            synchronized (UserManager.class) {
                if (instance == null) {
                    instance = new UserManager();
                }
            }
        }
        return instance;

    }


    public void setUserInfo(LoginResponse response) {
        LoginResponse.DataBean data = response.data;
        if (data != null) {
            L.d("---------------");
            user = new UserInfo();
            user.city_code = data.city_code;
            user.email = data.email;
            user.mobile = data.mobile;
            user.nick_name = data.nick_name;
            user.sex = data.sex;
            user.token = data.token;
            user.uid = data.uid;
            user.auth_status = data.auth_status;
            user.real_name = data.real_name;
            user.citizen_no = data.citizen_no;
           // user.deli_add = data.deli_add;
           // user.Picture = data.Picture;
            L.e(user.toString());
            SerializalUtils.setObject("set_user_info", user);

        } else {
            L.e("user is null......");
        }
    }

    public UserInfo getUser() {

        if (user == null) {
            user = SerializalUtils.getObject("set_user_info", UserInfo.class);
            if (user != null) {
                L.e(user.toString());
            }
        }
        return user;
    }

    public void setUserId(int uid) {
        SharedPreferences sp = UIUtils.getContext().getSharedPreferences("login_info", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putInt("user_id", uid);
        editor.commit();
    }

    public int getUserId() {
        int id;
        if (user!=null&&user.uid!=0){
            id=user.uid;
        }else {
            SharedPreferences sp = UIUtils.getContext().getSharedPreferences("login_info", Context.MODE_PRIVATE);
            id = sp.getInt("user_id", 0);
        }
        return id;
    }

    public void setUserToken(String token) {
        SharedPreferences sp = UIUtils.getContext().getSharedPreferences("login_info", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putString("user_token", token);
        editor.commit();
    }

    public String getUserToken() {
        String token="";
        if (user != null && !TextUtils.isEmpty(user.token)) {
            token = user.token;
        } else {
            SharedPreferences sp = UIUtils.getContext().getSharedPreferences("login_info", Context.MODE_PRIVATE);
            token = sp.getString("user_token", "");
        }
        return token;
    }

    public void clearUser() {
        SharedPreferences sp = UIUtils.getContext().getSharedPreferences("set_user_info", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        //  editor.remove("set_user_info");
        editor.clear();
        editor.commit();

        user=null;
    }

    public void clearLogin() {
        SharedPreferences sp = UIUtils.getContext().getSharedPreferences("login_info", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.clear();
        editor.commit();
    }


    public UserInfoRequest getRequestInfo() {
        UserInfoRequest info = new UserInfoRequest();
        UserInfo userInfo = getUser();
        info.city_code = userInfo.city_code;
        info.mobile = userInfo.mobile;
        info.sex = userInfo.sex;
        info.token = userInfo.getToken();
        info.uid = userInfo.uid;
        info.nick_name = userInfo.nick_name;
        info.email = userInfo.email;

        return info;
    }


    /* public void setObject(String key, Object object) {
        SharedPreferences sp = UIUtils.getContext().getSharedPreferences(key, Context.MODE_PRIVATE);

        //创建字节输出流
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        //创建字节对象输出流
        ObjectOutputStream out = null;
        try {
            //然后通过将字对象进行64转码，写入key值为key的sp中
            out = new ObjectOutputStream(baos);
            out.writeObject(object);
            String objectVal = new String(Base64.encode(baos.toByteArray(), Base64.DEFAULT));
            SharedPreferences.Editor editor = sp.edit();
            editor.putString(key, objectVal);
            editor.commit();

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (baos != null) {
                    baos.close();
                }
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }*/
}
